package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;
    @Autowired
    private MongoTemplate template;
    
    @Autowired
    private  IStrategyCommentService strategyCommentService;



    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        //设置时间
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public StrategyComment get(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentRepository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {

        //查询对象
        Query query = new Query();

        if (StringUtils.hasLength(qo.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        //查询总数

        Pageable pageable =PageRequest.of(qo.getCurrentPage()-1,
                qo.getPageSize(),Sort.Direction.ASC,"_id");

        return DBHelper.query(template, StrategyComment.class, query, pageable);
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //根据评论id获取评论对象
        StrategyComment strategyComment = this.get(cid);
        //获取评论对象中的点赞用户id列表
        List<String> list = strategyComment.getThumbuplist();
        //判断当前用户id是否在列表中
        if (list.contains(uid)){
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            list.remove(uid);
        }else {
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            list.add(uid);
        }
        this.update(strategyComment);
    }


}
